﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ModelLibrary;
using System.Text;

namespace WebC
{
    public partial class checkaccept : clsBaseForm
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Ajax.Utility.RegisterTypeForAjax(typeof(checkaccept));
            if (!IsPostBack)
            {
                loadInfo();
            }
        }

        public string sFileName = GlobleClass.UpFileType.CheckAccfile.ToString(); //"CheckAccfile";

        #region 载入信息

        public void loadInfo()
        {
            BuyDevice buy = null;
            string sModelName = "";
            DataSet ds = null;
            try
            {
                buy = new BuyDevice();
                //inniDdlData(ddlPayWay,buy.getEnumDdl(2));
                if (m_Id == 0)
                {
                    if (!checkModelRightById(244, ref sModelName, (clsBaseLib)buy))
                    {
                        errorUrl(string.Format("你没有“{0}”的权限,请与管理员联系！", sModelName));
                        return;
                    }
                }
                else
                {
                    if (Convert.ToInt32(Request["noedit"]) == 1)
                    {
                        if (!checkModelRightById(243, ref sModelName, (clsBaseLib)buy))
                        {
                            errorUrl(sModelName);
                            return;
                        }
                    }
                    else
                    {
                        if (!checkModelRightById(246, ref sModelName, (clsBaseLib)buy))
                        {
                            errorUrl(sModelName);
                            return;
                        }
                    }
                    ds = buy.getContractInfoById(m_Id);
                    hdselect.Value = buy.getContractSelectIds(m_Id);
                }
                sModelFullName = sModelName;

            }
            catch (Exception e)
            {
                errorUrl(Convert.ToString(e));
            }
            finally
            {
                if (buy != null)
                {
                    buy.Dispose();
                }
            }
            if (m_Id > 0)
            {
                if (ds != null)
                {
                    DataTable dt = ds.Tables[0];
                    if (dt.Rows.Count == 1)
                    {
                        //txtContractCode.Text = Convert.ToString(dt.Rows[0]["contractcode"]);
                        //txtContractName.Text = Convert.ToString(dt.Rows[0]["contractname"]);
                        //txtCompanyId.Text = Convert.ToString(dt.Rows[0]["companyid"]);
                        //txtCompanyUser.Text = Convert.ToString(dt.Rows[0]["companyuser"]);
                        //txtCheckDate.Text = Convert.ToDateTime(dt.Rows[0]["checkdate"]).ToString("yyyy-MM-dd");
                        //txtCompanyName.Text = Convert.ToString(dt.Rows[0]["companyname"]);
                        //txtContent.Text = Convert.ToString(dt.Rows[0]["contractcontent"]);
                        //txtStartDate.Text = Convert.ToDateTime(dt.Rows[0]["startdate"]).ToString("yyyy-MM-dd");
                        //txtEndDate.Text = Convert.ToDateTime(dt.Rows[0]["enddate"]).ToString("yyyy-MM-dd");
                        //txtMemo.Text = Convert.ToString(dt.Rows[0]["memo"]);
                        //txtUserDept.Text = Convert.ToString(dt.Rows[0]["userdeptname"]);
                        //txtUserName.Text = Convert.ToString(dt.Rows[0]["username"]);
                        //ddlPayWay.SelectedValue = Convert.ToString(dt.Rows[0]["payway"]);
                        //txtPayMoney.Text = Convert.ToString(dt.Rows[0]["paymoney"]);
                        //ddlIsJF.SelectedValue = Convert.ToString(dt.Rows[0]["isjf"]);
                        //tdupload.InnerHtml = Convert.ToString(dt.Rows[0]["contractfile"]);
                    }
                    dt = ds.Tables[1];
                    int iRowCount = dt.Rows.Count;
                    StringBuilder sbu = new StringBuilder();
                    for (int i = 0; i < iRowCount; i++)
                    {
                        sbu.Append(string.Format(@"<tr>
                        <td align=""left"" width=""120"">
                            <a href=""javascript:"" onclick=""insertdevice(this.parentNode.parentNode)"">+</a> <a href=""javascript:"" onclick=""removedevice(this.parentNode.parentNode)"">-</a><input style=""width: 92px"" type=""text"" onclick=""getdevicetype(this)"" value=""{0}"" /><input style=""width: 64px"" type=""hidden"" value=""{1}"" /></td>
                        <td width=""70"">
                            <input  style=""width: 70px"" type=""text"" value=""{2}"" /></td>
                        <td width=""70"">
                            <input  style=""width: 70px"" type=""text"" value=""{3}"" /></td>
                        <td width=""150"">
                            <input style=""width: 90px"" type=""text"" value=""{4}"" /></td>
                    </tr>", dt.Rows[i]["typename"], dt.Rows[i]["typeid"], dt.Rows[i]["devicenum"], dt.Rows[i]["singleprice"], dt.Rows[i]["modelinfo"]));
                    }
                    ltrDevice.Text = Convert.ToString(sbu);
                }
            }
        }

        #endregion

        int m_Id
        {
            get
            {
                return Convert.ToInt32(Request["id"]);
            }
        }

        #region 获得设备

        [Ajax.AjaxMethod]
        public DataTable getSelectDeviceInfo(string sIds)
        {
            BuyDevice buy = null;
            DataTable dt = null;
            try
            {
                buy = new BuyDevice();
                dt = buy.getResultDeviceInfo(sIds);
            }
            catch
            {
                dt = null;
            }
            finally
            {
                if (buy != null)
                {
                    buy.Dispose();
                }
            }
            return dt;
        } 

        #endregion

        #region 保存信息

        [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
        public bool saveData(int iContractId,string sCode,string sCheckDate, string sContractName, int iPayWay, string sPayMoney, string sUserDept, string sUName
            ,int iIsJF, int iCompanyId, string sCompanyUser, string sStartDate, string sEndDate, string sContent, string sMemo, string sFiles, string sSelectIds, string[] sDevices)
        {
            BuyDevice buy = null;
            bool bResult = false;
            sCode = sCode.Replace("'", "''");
            sCheckDate = sCheckDate.Replace("'", "''");
            sContractName = sContractName.Replace("'", "''");
            sPayMoney = sPayMoney.Replace("'", "''");
            sUserDept = sUserDept.Replace("'", "''");
            sCompanyUser = sCompanyUser.Replace("'", "''");
            sStartDate = sStartDate.Replace("'", "''");
            sEndDate = sEndDate.Replace("'", "''");
            sContent = sContent.Replace("'", "''");
            sMemo = sMemo.Replace("'", "''");
            sFiles = sFiles.Replace("'", "''");
            try
            {
                buy = new BuyDevice();

                for (int i = 0; i < sDevices.Length; i++)
                {
                    sDevices[i] = sDevices[i].Replace("'", "''");
                }
                bResult = buy.setContractInfo(iContractId,sCode,sCheckDate, sContractName, iPayWay, sPayMoney, sUserDept, sUName
            , iIsJF, iCompanyId, sCompanyUser, sStartDate, sEndDate, sContent, sMemo, sFiles, sSelectIds, sDevices);
            }
            catch
            {
                bResult = false;
            }
            finally
            {
                if (buy != null)
                {
                    buy.Dispose();
                }
            }
            return bResult;
        }

        #endregion
    }
}